package com.stillalive.springbootbooksystem.Service;

import com.stillalive.springbootbooksystem.Model.BookInfo;
import com.stillalive.springbootbooksystem.Model.PageRequest;
import com.stillalive.springbootbooksystem.Model.ResponseResult;
import com.stillalive.springbootbooksystem.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
// 业务逻辑层
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        // 获取总图书数
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.selectBookByPage(pageRequest);
        // 中文状态
        for(BookInfo bookInfo : bookInfos){
            if(bookInfo.getStatus() == 1){
                bookInfo.setStatusCN("可借阅");
            }else{
                bookInfo.setStatusCN("不可借阅");
            }
        }

        return new ResponseResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> bookIds) {
        return bookMapper.batchDelete(bookIds);
    }
}
